//
//  BRInviteHistoryViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2022/1/21.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRInviteHistoryViewController.h"
#import "BRInviteRecordListCell.h"

@interface BRInviteHistoryViewController ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
    NSInteger _offset;
}

@property (nonatomic,strong) NSMutableArray *dataList;


@end

@implementation BRInviteHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.emptyLYView setImage:UIImageMake(@"error_invitation")];
    self.emptyLYView.titleStr = Localized(@"error_nofriendInvited");
    self.dataList = [NSMutableArray array];
    [self setUI];
    
    [self addRefreshActionForView:self.tableView];
    [self addLoadMoreActionForView:self.tableView];
    
    self.tableView.mj_footer.hidden = YES;
    [self requePointList];
}

- (void)handleListRefreshAction {
    _offset = 0;
    [self requePointList];
}

- (void)handleListLoadMoreAction {
    _offset += kPageSize;
    [self requePointList];
}

#pragma mark - request --
- (void)requePointList{
    
    NSDictionary *dic = @{@"offset":@(_offset)};
    NSString *url = [BRAPI inviteHistory];
    [self.tableView ly_startLoading];
    [[BRRequestManager shared] GET:url params:dic succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        NSLog(@"%@",dataDict);
        if (self->_offset == 0) {
            [self.dataList removeAllObjects];
        }
        NSInteger allC = self->_dataList.count;
        NSArray *list = [BRInviteHistoryModel mj_objectArrayWithKeyValuesArray:dataDict[@"data"]];
        [self.dataList addObjectsFromArray:list];
        if (self.dataList.count < allC + kPageSize || self.dataList.count == 0 ) {
            self.tableView.mj_footer.hidden = YES;
        }
        
        [self.tableView reloadData];
        [self showBREmptyView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
        
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        self.tableView.mj_footer.hidden = YES;
        [self showBRErrorView:self.tableView];
        [self handleEndRefreshForView:self.tableView];
    }];
}

#pragma mark - tableView Delegate --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return 5;
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BRInviteRecordListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRInviteRecordListCell"];
    BRInviteHistoryModel *model = self.dataList[indexPath.row];
    cell.dto = model;
    
   
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    

    
}

#pragma mark - UI --
- (void)setUI {
    
    [self br_createTop:Localized(@"invite_referralRecord")];
    
    self.tableView.estimatedRowHeight = 134;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.tableView registerClass:[BRInviteRecordListCell class] forCellReuseIdentifier:@"BRInviteRecordListCell"];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 14)];
    self.tableView.tableFooterView = foot;
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
